// pages/mine/collect/index.js
const app = getApp()
const utils = require('../../../utils/util.js')
import Toast from '../../../vant-weapp/dist/toast/toast'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navHeight: app.globalData.navHeight,
    schemeList:[],
    templateList:[],
    myLiveGroundList:[],
    topActiveIndex: 0,
    rightList:[],
    leftList:[],
    topTabsList: [
      {
        name:'方案',
        id:0,
      },
      {
        name:'样板间图集',
        id:5,
      },
      {
        name:'样板间视频',
        id:6,
      },
      {
        name:'实拍图集',
        id:1,
      },
      {
        name:'实拍视频',
        id:2,
      },
      {
        name:'设计vs实得',
        id:3,
      },
      {
        name:'装修攻略',
        id:4,
      }
      ],
    toCrmParams:'',
    page:1,
    rows: 10,
    contentPage: 1,
    contentRows: 10,
    isRefresh:false,
    hasNoMore:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      toCrmParams: JSON.stringify({ url: '/pages/mine/main/index'})
    })
    this.getMyCollection()
    // this.handleGetMyContributePage()
    this.getMyContentCollection()
    this.handleGetMyRealGroundList()
  },
 // GetMyContentCollection 获取装修攻略收藏列表
  getMyContentCollection:function( flag = false){
    const _this = this 
    const openId = wx.getStorageSync('openId') || ''

    utils.$http(app.globalData.api.GetMyContentCollection+'/'+openId,{
      // openId: openId,
      page: this.data.contentPage,
      rows: this.data.contentRows
    },'POST',true,'JSON').then(({ data, success, msg }) => {
      if (success === 1) {
        const { records = [] } = data
        if(flag) {
          if(records.length === 0) {
            this.setData({
              hasNoMore:true
            })
          } else {
            const list = this.data.schemeList.concat(records)
            _this.setData({
              isRefresh: false,
              schemeList: list,
            })
          }
        } else {
          _this.setData({
            isRefresh:false,
            hasNoMore: false,
            schemeList: records,
          })
        }
      }
    }).catch(()=> {
      this.setData({
        isRefresh: false
      })
    })
  },
  //获取实景落地分页数据
  handleGetMyRealGroundList(flag=false){
    const {contentPage, contentRows} =this.data
    const userInfo=wx.getStorageSync('userInfo')
    utils.$http(app.globalData.api.getRealMyCollectGroundList,{
      // openId: openId,
      userName:userInfo.unionId,
      page:contentPage,
      rows:contentRows
    },'POST',true,'JSON').then(({ data, success, msg }) => {
      if(success){
        const { records = [] } = data
        if(flag) {
          if(records.length === 0) {
            this.setData({
              hasNoMore:true
            })
          } else {
            const list = this.data.myLiveGroundList.concat(records)
            this.setData({
              isRefresh: false,
              myLiveGroundList: list,
            })
          }
        } else {
          this.setData({
            isRefresh:false,
            hasNoMore: false,
            myLiveGroundList: records,
          })
        }
      }
    })
  },
  handleCancelCollect(){
    this.setData({
      contentPage:1
    })
    this.handleGetMyContributePage()
  },
  //获取我的实景投稿分页数据
  handleGetMyContributePage(flag=false){
    const userInfo=wx.getStorageSync('userInfo')
    const {contentPage,contentRows,topActiveIndex}=this.data
    let types='0'
    if(topActiveIndex===1){
      types='0'
    }else if(topActiveIndex===2){
      types='1'
    }else if(topActiveIndex===5){
      types='2'
    }else if(topActiveIndex===6){
      types='3'
    }else{
      types='0'
    }
    const list={
      page:contentPage,
      rows:contentRows,
      userId:userInfo.unionId,
      contributeType:types,
      approveState:1,
      upState:0,
    }
    utils.$http(app.globalData.api.realityContributePage,list,'POST',true,'JSON').then(({ data, success, msg }) => {
      if(success){
        const { records = [] } = data
        const {leftList=[],rightList=[]} =this.data
        if(flag) {
          if(records.length === 0) {
            this.setData({
              hasNoMore:true,
            })
          } else {
            if(records.length===1){
              this.setData({
                isRefresh: false,
                leftList:leftList.concat(records)
              })
             }else{
              records.forEach((item,index)=>{
                 if((index+1)%2===0){
                   rightList.push(item)
                 }else{
                   leftList.push(item)
                 }
               })
               this.setData({
                isRefresh: false,
                 rightList,
                 leftList
               })
             }
          }
        } else {
            const lList=[],rList=[];
            records.forEach((item,index)=>{
               if((index+1)%2===0){
                 rList.push(item)
               }else{
                 lList.push(item)
               }
             })
          this.setData({
            isRefresh:false,
            hasNoMore: false,
            leftList:lList,
            rightList:rList
          })
        }
      }
    })
  },
  // GetMyCollection 获取方案收藏列表
  getMyCollection:function( flag = false){
    const _this = this 
    const openId = wx.getStorageSync('openId') || ''

    utils.$http(app.globalData.api.GetMyCollection,{
      openId: openId,
      page: this.data.page,
      rows: 5
    },'POST',true,'JSON').then(({ data, success, msg }) => {
      if (success === 1) {
        const { records = [] } = data
        if(flag) {
          if(records.length === 0){
            this.setData({
              hasNoMore:true
            })
          } else {
            const list = this.data.templateList.concat(records)

            _this.setData({
              isRefresh:false,
              templateList: list,
            })
          }
        } else {
          _this.setData({
            isRefresh:false,
            hasNoMore: false,
            templateList: records,
          })
        }
      }
    }).catch(()=> {
      this.setData({
        isRefresh: false
      })
    })
  },
  // 切换tab
  topTabsChange(e) {
    // console.log(e)
    const index = e.detail.name
    this.setData({
      topActiveIndex: index,
      page:1,
      contentPage:1,
      leftList:[],
      rightList:[]
    })
    this.handleGetMyContributePage()
  },

  goBack:function(){
    wx.navigateBack({
      
    })
  },
  goHome: function () {
    wx.switchTab({
      url: '/pages/home/main/index'
    })
  },
  //取消实景落地收藏
  handleCancelGroundColl(){

  },
  // 取消方案收藏
  cancelCollect(e){
    const item = e.currentTarget.dataset.item
    const type = this.data.topActiveIndex
    const openId =  wx.getStorageSync("openId") || ''
    const arr=[]
    arr.push( item.programmeId)
    if (type === 0) {
     const  param = {
        openId: openId,
        programmeIds:arr
        // programmeId: item.programmeId,
       }
      utils.$http(app.globalData.api.CollectOrCancel, param, "POST", false, "JSON").then(({ success})=>{
        if (success === 1) {
          this.data.page=1
          setTimeout(()=>{
            this.getMyCollection()
          },300) 
        }
      }) 
    } else if(type===2) {
      const params = {
        userId: openId,
        articleId: item.id,
        type: 2
      }
      utils.$http(app.globalData.api.SetArticleCollectOrCancel, params, "POST", false, "JSON").then(({ success})=>{
        if (success === 1)  {
          setTimeout(()=>{
            this.getMyContentCollection()
          },300) 
        }
      }) 
    }
  },
  childListMethod(item){
    const userInfo = wx.getStorageSync('userInfo')
    const str={
      pid:item.detail.programmeId,
      userName:userInfo.unionId
    }
    utils.$http(app.globalData.api.getRealMyGroundCancelCollectRecords, str, 'GET', true, 'JSON').then(res => {
      if(res.success){
      wx.showToast({ icon: 'success', title: '取消收藏成功', duration: 1000 })
      this.data.contentPage=1
      setTimeout(()=>{
        this.handleGetMyRealGroundList()
      },300) 
      
    }
    })
  },
  // 跳详情页
  footerListItemClick:function(e){
    const item = e.currentTarget.dataset.item
    const type = this.data.topActiveIndex

    let url = ''
    if(type === 0){
      url = '/pages/Details/main/index?id=' + item.programmeId
    }
    // console.log('url:',url)
    wx.navigateTo({
      url: url
    })

  },
  loadNewData: function() {
    this.setData({
      isRefresh :true
    })
    const type = this.data.topActiveIndex
    if(type === 0) {
      this.data.page = 1
      this.getMyCollection()
    }else {
      this.data.contentPage = 1
      this.getMyContentCollection()
      this.handleGetMyRealGroundList()
      this.handleGetMyContributePage()
    }
  },
  loadMoreData:function() {
    const type = this.data.topActiveIndex
    if(type === 0) {
      this.setData({
        page: this.data.page + 1
      })
      this.getMyCollection(true)
    } else{
      this.setData({
        contentPage: this.data.contentPage + 1
      })
      this.getMyContentCollection(true)
      this.handleGetMyRealGroundList(true)
      this.handleGetMyContributePage(true)
    }
  },

  // 跳内容详情页
  toDetailPage(e) {
    const item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: `/pages/guide/index/index?id=${encodeURIComponent(item.id)}`
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
   
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})